package org.tech4j.schedule.powerjob;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.Environment;
import org.tech4j.common.spi.ExtensionLoader;
import org.tech4j.edi.api.factory.RetryHandleFactory;
import org.tech4j.edi.schedule.common.ScanRetryAlarmService;
import tech.powerjob.worker.core.processor.ProcessResult;
import tech.powerjob.worker.core.processor.TaskContext;
import tech.powerjob.worker.core.processor.sdk.BasicProcessor;

import java.util.List;

/**
 * @program: tech4j
 * @description:
 * @author: YuKai Fan
 * @create: 2025/3/10 16:34
 **/
public class AlarmRetryJobHandler implements BasicProcessor {
    private static final Logger log = LoggerFactory.getLogger(AlarmRetryJobHandler.class);

    private final String env;
    private final ScanRetryAlarmService scanRetryAlarmService;

    public AlarmRetryJobHandler(String env, ScanRetryAlarmService scanRetryAlarmService) {
        this.env = env;
        this.scanRetryAlarmService = scanRetryAlarmService;
    }

    @Override
    public ProcessResult process(TaskContext taskContext) throws Exception {
        log.info("开始执行EDI告警");
        List<RetryHandleFactory> retryHandleFactoryList =
                ExtensionLoader.getExtensionLoader(RetryHandleFactory.class).getJoins();
        for (RetryHandleFactory retryHandleFactory : retryHandleFactoryList) {
            List<String> retryTypeList = retryHandleFactory.getRetryType();
            scanRetryAlarmService.scanRetryAlarm(env, retryTypeList);
        }
        return new ProcessResult(true);
    }
}
